//
//  YeeCacheManger.m
//  YeeShopProject
//
//  Created by CoderYee on 2017/3/9.
//  Copyright © 2017年 CoderYee. All rights reserved.
//

#import "YeeCacheManger.h"

@implementation YeeCacheManger

static YeeCacheManger *_intstance=nil;

+(YeeCacheManger*)manger
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _intstance=[[self alloc] init];
    });
    return _intstance;
}

//异步获取缓存的大小
-(void)getCacheSizeSuccessBlock:(void(^)(CGFloat cacheSize))successBlock failBlock:(void(^)(NSError*error))failBlock
{
   
    NSString *cachePath =[NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :cachePath])
    {
        if (failBlock)
        {
            failBlock([NSError new]);
        }
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :cachePath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    
    if (successBlock) {
        successBlock(folderSize);
    }
}
-( float)readCacheSize
{
    NSString *cachePath =[NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    return [self folderSizeAtPath :cachePath];
}
- (float)folderSizeAtPath:(NSString *) folderPath
{
    
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    return folderSize;
}
- ( long long )fileSizeAtPath:(NSString *) filePath
{
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
    }
    return 0;
}
//清除所有缓存
-(void)removeAllCache
{
   
    NSString * cachePath =[NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    //NSLog ( @"cachpath = %@" , cachePath);
    for ( NSString * p in files)
    {
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
        }
    }
    
}
@end
